package com.rh.exception;

import com.rh.result.ResultVo;
import com.rh.utils.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHanlder {
    //全局异常处理
    @ExceptionHandler(value = Exception.class)
    @ResponseBody //返回Json数据
    public ResultVo error(Exception e){
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return ResultVo.error().message("执行了全局异常处理...");
    }

    /**
     * 特定异常处理方法
     */
    @ExceptionHandler(value = {ArithmeticException.class})
    @ResponseBody
    public ResultVo doExceptionArit(Exception e){
        log.error(ExceptionUtil.getMessage(e));
       e.printStackTrace();
       return ResultVo.error().message("分母不能为0");
    }
    /**
     * 自定义异常
     */
    @ExceptionHandler(value = {EducationException.class})
    @ResponseBody
    public ResultVo educationException(EducationException e){
        log.error(ExceptionUtil.getMessage(e));
        e.printStackTrace();
        return ResultVo.error().code(e.getCode()).message(e.getMsg());
    }
}
